#pragma once
#include "../MSPIX/MSPIX.h"


#define MAX_COLOR_CHANNEL 3
#define MAX_COLOR_DIMENTION 256


typedef struct NODE
{
	float th;
	int   dim;
	int   start[MAX_COLOR_CHANNEL];
	int   end[MAX_COLOR_CHANNEL];
	int   size[MAX_COLOR_CHANNEL];
	int   color[MAX_COLOR_CHANNEL];
	int   pixel_num;
}NODE;



typedef struct KDTREE
{
	NODE* p_node;
	int leaf_num;
	int layer_num;
	int max_node_num;
	int valid_node_num;
}KDTREE;


class MCUT
{
public:
	MCUT(int color_num);
	~MCUT(void);

private:
	unsigned int* p_hist1d[MAX_COLOR_CHANNEL];
	void copy_node(NODE* p_src, NODE* p_dst);
	void spit_node(NODE* p_parent_node, NODE* p_left_node, NODE* p_right_node);
	void get_child_id(int parent_id, int* p_left_child_id, int* p_right_child_id);
	void get_max_dimesion(NODE* p_node);
	void init_root_node(NODE* p_root);
	void find_median(NODE* p_node);

	void create_kd_tree();
	int  index_kd_tree(unsigned char* p_pixel);
	void create_1d_hist_for_spix(S_Pix* p_spix, int spix_num);
	void create_1d_hist_for_color_image();
	void find_suit_color_for_leaf_nodes_by_src_img();
	void find_suit_color_for_leaf_nodes_by_src_spix(S_Pix* p_spix, int spix_num);
public:
	int img_width;
	int img_height;
	unsigned char* p_src_img;

	KDTREE kd_tree;
	void init(unsigned char* p_src_img, int width, int height);
	void run_for_image();
	void run_for_superpixel(S_Pix* p_spix, int spix_num);
	void map_src_img_to_cut_img(unsigned char* p_dst_img);
	void map_src_img_to_cut_img_for_superpixel(S_Pix* p_spix, int spix_num, short* p_spix_label_map, unsigned char* p_dst_img);

};

